package com.LeeCode;

/**
 * 包含所有三种字符的子字符串数目
 */

public class Code1358 {
    public static void main(String[] args) {
        String s = "abcc";
        System.out.println(new Code1358().numberOfSubstrings(s));
    }

    public int numberOfSubstrings(String s) {
        char[] chars = s.toCharArray();
        int[] cnt = new int[3];
        int n = chars.length, ans = 0, right = 0, count = 0;
        for (int left = 0; left < n; left++) {
            while (right < n && count < 3) {
                cnt[chars[right] - 'a']++;
                if (cnt[chars[right] - 'a'] == 1) count++;
                right++;
            }

            if (count == 3) {
                ans += n - right + 1;
            }
            cnt[chars[left] - 'a']--;
            if (cnt[chars[left] - 'a'] == 0) count--;
        }
        return ans;
    }
}
